import {toCamKey} from "@/utils/wanfeng";


export function LimsFilters(){
    this.logic = 'and'
    this.filter = []
    this.childs = []
    this.orderBy = []
    let that = this
    this.addFilter = function (column, operator ,value){
        that.filter.push({field:column, operator:operator, value:value})
        return that
    }
    this.eq = function(column, value){
        this.addFilter(column, 'EQUAL', value)
        return this
    }
    this.notEq = function (column, value){
        this.addFilter(column, 'NOT_EQUAL', value)
        return this
    }
    this.like = function (column, value){
        this.addFilter(column, 'contains', value)
        return this
    }
    this.order = function (column, dir){
        this.orderBy.push({orderBy: column, dir: dir})
        return this
    }
    this.orderByAsc = function (column){
        this.order(column,'ASC')
        return this
    }
    this.orderByDesc = function (column){
        this.order(column, 'DESC')
        return this
    }
    this.addChild = function (filter){
        this.childs.push(filter)
        return this
    }
    this.exec = function (){
        return {
            logic: that.logic,
            filter: that.filter,
            childs: that.childs,
            orderBy: that.orderBy
        }
    }
}

function OrderBy(field, dir){
    this.field = field
    this.dir = dir
}

export function LimsConfig(tableName){
    this.tableName = tableName
    this.securityField = ''
    this.column = []
    this.childs = []
    this.filters = new LimsFilters()
    this.orderBy = []
    this.functionName = ''
    this.currentPage = 0
    this.pageSize = 0

    this.addOrderBy = function (field, dir){
        this.orderBy.push(new OrderBy(field, dir))
    }
    this.addChild = function (child){
        this.childs.push(child)
    }


}


/**
 * 保存表单数据
 * @param tableName 数据库表名称
 * @param model 表单数据，字段使用实体类中的字段
 * @param operator 操作类型： add, edit
 * @param childs 细表
 */
export function LimsFormConfig(tableName, model, operator){
    this.tableName = tableName
    this.model = toCamKey(model)
    this.oper = operator
    this.childs = []
    this.workflowFields = ''
    this.workflowValues= ''
    this.addChild = function (tableName, rows){
        this.childs.push(new LimsFormChild(tableName, rows))
    }
}

export function LimsFormChild(tableName, rows){
    this.tableName = tableName
    this.rows = toCamKey(rows)
}